﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using Rabbit.API.Application.Command.FinishStep;

namespace Rabbit.API.Application.Handler.FinishStep
{
    public class FinishStepUpdateHandler:IRequestHandler<FinishStepUpdateCommand, APIResult<int>>
    {
        private readonly IRepository<FinishStepModel> finishstepway;
        private readonly IMapper mapper;
        private readonly ILogger<FinishStepGetHandler> log;

        public FinishStepUpdateHandler(IRepository<FinishStepModel> finishstepway, IMapper mapper, ILogger<FinishStepGetHandler> log)
        {
            this.finishstepway = finishstepway;
            this.mapper = mapper;
            this.log = log;
        }

        /// <summary>
        /// 修改深加工状态
        /// </summary>
        /// <param name="request">参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        public async Task<APIResult<int>> Handle(FinishStepUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> res = new APIResult<int>();
            try
            {
                //var value = killway.GetValue(request.Id);
                var value = mapper.Map<FinishStepModel>(request);
                var result = await finishstepway.Updateway(value);
                if (result > 0)
                {
                    log.LogInformation($"深加工信息修改成功");
                    res.Message = "深加工信息修改成功";
                    res.Num = StateCode.OK;
                    res.Data = result;

                }
                else
                {
                    res.Message = "深加工信息修改失败";
                    res.Num = StateCode.Fail;
                }
                return res;

            }
            catch (Exception ex)
            {
                log.LogError($"深加工信息修改发生异常{ex.Message}");
                throw;
            }
        }
    }
}
